<?php
/**
 * 充值业务逻辑类
 *
 * @author Alen
 * @since 1.0.0 2013-12-14
 */
class SoBizRecharge extends SoBizBase {
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->dm->tablename='tfinancerecharge';
    }
    
    /**
     * 充值处理
     * @param type $uname
     * @param type $amount
     * @param type $bank_transaction_sn
     * @param type $cardholder
     * @param type $bankname
     * @param type $remark
     * @return type
     * @since 1.0.0 2013-12-16
     */
    public function recharge($uname,$amount,$bank_transaction_sn,$cardholder,$bankname,$remark){
        
	$this->dm->add_param('BankTransactionSN',$bank_transaction_sn,'交易流水号未填写');
	$this->dm->add_param('BankCardHolder',$cardholder,'付款人未填写');
	$this->dm->add_param('BankName',$bankname,'开户行名称未填写');
	$this->dm->add_param('Amount',$amount,'充值金额未填写','int');
	$this->dm->add_param('UserRemark',$remark);
	$this->dm->add_param('UserName',$uname);
	$this->dm->add_param('SerialNo',get_serialstring());
        if($this->dm->has_error()) return $this->dm->get_error ();
        
        // 读取账户信息
        $bizacc=new SoBizFinanceAccount();
        $acc=$bizacc->read_one($uname);
        if($acc['State']!=1) return '账户状态异常';
        
	$result=$this->dm->insert();
	if($result){
            $acc['FrozenBlance']=$acc['FrozenBlance']+$this->dm->Amount;
		// 添加一条交易记录
		$this->dm->insert_record('tfinancetransactionlog','SerialNo,MasterSN,Amount,TransactionType,Blance,FrozenBlance,UserName,UserRemark',sprintf('\'%s\',\'%s\',%u,\'IN_RECHARGE\',%u,%u,\'%s\',\'%s\'',get_serialstring(),$this->dm->SerialNo,$this->dm->Amount,$acc['Blance'],$acc['FrozenBlance'],$uname,$this->dm->UserRemark));
		// 更新用户账户资金数据
		$this->dm->update_records('tfinanceaccount',sprintf('FrozenBlance=%u',$acc['FrozenBlance']),"UserName='$uname'");
	}
        return $result?'SUCCESS':$this->dm->get_error();
    }
    
    /**
     * 审核充值
     * @param type $sn
     * @param type $uname
     * @param type $state
     * @param type $amount
     * @param type $remark
     * @return type
     * @since 1.0.0 2013-12-14
     */
    public function confirm_by_admin($sn,$uname,$state,$amount,$remark){
	$this->dm->add_param('State',$state,'未选择审核结果','int');
	$this->dm->add_param('AdminRemark',$remark);	
	$this->dm->add_param('ModifiedTime',date('Y-m-d H:i:s'));
	$result=$this->dm->update(sprintf('SerialNo=\'%s\'',$sn));
	if($result){
                switch($state){
                case 1:
			$this->dm->update_records('tfinanceaccount',sprintf('FrozenBlance=FrozenBlance-%u, Blance=Blance+%u',$amount,$amount),'UserName=\''.$uname.'\'');		
			$this->dm->update_records('tfinancetransactionlog',sprintf('FrozenBlance=FrozenBlance-%u, Blance=Blance+%u,State=%u,ModifiedTime=\'%s\',AdminRemark=\'%s\'',$amount,$amount,$this->dm->State,$this->dm->ModifiedTime,$this->dm->AdminRemark),"MasterSN='$sn'");
                    break;
                case 4:
			$this->dm->update_records('tfinanceaccount',sprintf('FrozenBlance=FrozenBlance-%u',$amount),'UserName=\''.$uname.'\'');		
			$this->dm->update_records('tfinancetransactionlog',sprintf('FrozenBlance=FrozenBlance-%u,State=%u,ModifiedTime=\'%s\',AdminRemark=\'%s\'',$amount,$this->dm->State,$this->dm->ModifiedTime,$this->dm->AdminRemark),"MasterSN='$sn'");
                    break;
                case 0:
                    break;
            }
	}
        return $result?'SUCCESS':$this->dm->get_error();        
    }
    
    /**
     * 读取一条记录
     * @param type $sn
     * @param type $search
     * @return type
     * @since 1.0.0 2013-12-14 sutroon
     */
    public function read_one($sn,$search='SerialNo=\'%s\''){        
        return $this->dm->select_one_row('',  sprintf($search,$sn));
    }
    
    /**
     * 获取分页数据
     * @param type $pager_text
     * @param type $dataset
     * @param type $pageid
     * @param type $keyword
     * @since 1.0.0 2012-12-14 sutroon
     * @example 
        $dataset=null;
        $pager_text='';
        $biz->get_paging_data($pager_text, $dataset, request_numeric('page'), request_string('keyword'));
     */
    public function get_paging_data(&$pager_text,&$dataset,$pageid,$keyword){        
        // 获取分页数据
        $pager=new SoDataPager($pageid);
	$sqlc=$keyword?' where SerialNo like \'%'.$keyword.'%\' or UserName like \'%'.$keyword.'%\' or UserRemark  like \'%'.$keyword.'%\' or BankTransactionSN like \'%'.$keyword.'%\' ':'';
        $dataset=$this->dm->select_pagingset($pager->pageid,$pager->pagesize,$pager->recordcount,'tfinancerecharge '.$sqlc,'CreatedTime desc','*');               
        $pager->set_dataset($dataset,$pager->recordcount);
        $pager_text=$pager->get_pager_text(set_url_param(array('page'=>'.pageid.')));
    }    
    
    /**
     * 获取充值总金额
     * @return type
     * @since 1.0.0 2013-12-14 sutroon
     */
    public function get_amount_all(){
        return $this->dm->select_value('', '','sum(Amount)','0.00');
    }
    
    /**
     * 获取已处理金额
     * @return type
     * @since 1.0.0 2013-12-14 sutroon
     */
    public function get_amount_confirmed(){
        return $this->dm->select_value('', 'State=1','sum(Amount)','0.00');
    }
    
    /**
     * 获取待处理金额
     * @return type
     * @since 1.0.0 2013-12-14 sutroon
     */
    public function get_amount_unconfirmed(){
        return $this->dm->select_value('', 'State=0','sum(Amount)','0.00');
    }
}
